package com.tianfei.crowd.mvc.controller;


import com.github.pagehelper.PageInfo;
import com.tianfei.crowd.entity.Role;
import com.tianfei.crowd.service.api.RoleService;
import com.tianfei.crowd.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: Herz
 * @date: 2021/7/13 16:04
 */
@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ResponseBody
    @RequestMapping("/role-remove-id-list.json")
    public ResultEntity<String> removeRoleByIdList(@RequestBody List<Integer> roleIdList){

        roleService.deleteRole(roleIdList);

        return ResultEntity.successWithOutData();
    }


    @ResponseBody
    @RequestMapping("/role-update.json")
    public ResultEntity<String> update(
            @RequestParam("id") Integer roleId,
            @RequestParam("name") String roleName
    ){

        roleService.updateRole(roleId,roleName);

        return ResultEntity.successWithOutData();
    }


    @ResponseBody
    @RequestMapping("/role-save.json")
    public ResultEntity<String> saveRole(@RequestParam("name") String roleName){

        roleService.saveRole(roleName);

        return ResultEntity.successWithOutData();
    }


    @PreAuthorize("hasRole('部长')")    // 具备部长角色才能访问 Role分页数据页面
    @ResponseBody
    @RequestMapping("/role-get-page-info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword
    ) {

        // 调用service 里的方法获取数据信息
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);

        // 封装到 ResultEntity 对象里
        return ResultEntity.successWithData(pageInfo);
    }


}
